using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using PowerSearch.AppServices;
using PowerSearch.Core;

namespace PowerSearch.ICS.UI
{
	public class ImageAdapter : BaseAdapter
	{
		Context _context;

		IDictionary<int, KeyValuePair<int, string>> _providers = new Dictionary<int, KeyValuePair<int, string>> ();

		public ImageAdapter(Context context)
		{
			_context = context;
			InitializeProviders ();
		}

		public override int Count {
			get {
				return _providers.Count;
			}
		}

		public override Java.Lang.Object GetItem (int position)
		{
			return new SearchResult (_providers[position].Value);
			//return GetSearchResult (_providers[position].Value);
		}

		public override long GetItemId (int position)
		{
			return 0;
		}

		public override View GetView (int position, View convertView, ViewGroup parent)
		{
			ImageView imageView;

			if (convertView == null) {
				imageView = new ImageView (_context);
				imageView.LayoutParameters = new GridView.LayoutParams (150, 150);
				imageView.SetScaleType (ImageView.ScaleType.CenterCrop);
				imageView.SetPadding (8, 8, 8, 8);
			} else
				imageView = (ImageView)convertView;

			imageView.SetImageResource (_providers[position].Key);
			return imageView;
		}

		private SearchResult GetSearchResult(string provider)
		{
			var service = new SearchService ();
			var feedResults = service.GetSearchResults(string.Empty, new string[] {provider});

			var results = new StringBuilder();
			foreach (Feed feed in feedResults) {
				results.AppendLine("Title: " + feed.Title);
				results.AppendLine("Description: " + feed.Description);
				results.AppendLine("ClickUrl: " + feed.ClickUrl);
				results.AppendLine ("---");
			}

			return new SearchResult (results.ToString());
		}

		private void InitializeProviders()
		{
			_providers.Add(0, new KeyValuePair<int, string>(Resource.Drawable.BBC, "BBC"));
			_providers.Add(1, new KeyValuePair<int, string>(Resource.Drawable.CNN, "CNN"));
			_providers.Add(2, new KeyValuePair<int, string>(Resource.Drawable.ESPN, "ESPN"));
			_providers.Add(3, new KeyValuePair<int, string>(Resource.Drawable.HuffingtonPost, "HuffingtonPost"));
			_providers.Add(4, new KeyValuePair<int, string>(Resource.Drawable.People, "PeopleMagazine"));
			_providers.Add(5, new KeyValuePair<int, string>(Resource.Drawable.TMZ, "TMZ"));
			_providers.Add(6, new KeyValuePair<int, string>(Resource.Drawable.Yahoo, "Yahoo"));
		}
	}

	public class SearchResult : Java.Lang.Object
	{
		public readonly string Result;

		public SearchResult (string result)
		{
			Result = result;
		}
	}
}